package com.fowo.api.user.service.impl;

import com.fowo.api.sys.entity.SysUser;
import com.fowo.api.sys.service.SysUserService;
import com.fowo.api.user.model.SysUserDetails;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        final SysUser sysUser = userService.getByUsername(username);
        if(sysUser == null) {
            throw new UsernameNotFoundException("未找到此帐号");
        }
        SysUserDetails sysUserDetails = new SysUserDetails();
        BeanUtils.copyProperties(sysUser, sysUserDetails);

        return sysUserDetails;
    }

}
